/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hathor.conf;

import com.hathor.batch.BatchFileGenerator;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 *
 * @author Kenneth
 */
public class PropertyLoader {

    private static Properties config = new Properties();

   
    public static void loadDefaultProperties() {
        InputStream in = null;
        try {
            in = BatchFileGenerator.getInstance().getClass().getResourceAsStream("/default.properties");
            config.load(in);
            in.close();
        } catch (IOException ex) {
        } finally {
            if (in != null) {
                in = null;
            }
        }
    }

    public static void loadUserProperties() {
        File userProperties = new File("INSERT STANDARD FILE HERE");
        if (userProperties.exists()) {
            InputStream stream = null;
            try {
                stream = new FileInputStream(userProperties);
                config.load(stream);
                stream.close();
            } catch (IOException ex) {
            } finally {
                if (config == null) {
                    loadDefaultProperties();
                }
            }
        } else {
            loadDefaultProperties();
        }
    }

    public static int getMotherShipPort() {
        return Integer.parseInt(config.getProperty("mothership.port"));
    }

    public static HashMap<String, Integer> getHostMap() {
        HashMap<String, Integer> hostMap = new HashMap<>();
        Enumeration<?> allProperties = config.propertyNames();
        while (allProperties.hasMoreElements()) {
            String property = (String) allProperties.nextElement();
            String[] host;
            int hostPort;
            if (property.startsWith("host")) {
                host = property.split(":");
                try {
                    hostPort = Integer.parseInt(host[1]);
                    hostMap.put(host[0], hostPort);
                } catch (Exception e) {
                    System.out.println("Invalid host :" + property);
                }
            }
        }
        return hostMap;
    }

    //FOR RESOURCE POOLING
    public static String getMappingDriveLetter() {
        return config.getProperty("server.mapping.driveletter");
    }

    public static String getServerName() {
        return config.getProperty("server.mapping.name");
    }

    public static String getServerShareName() {
        return config.getProperty("server.mapping.share");
    }

    public static String getServerShareUser() {
        return config.getProperty("server.mapping.share.user");
    }

    public static String getServerSharePassword() {
        return config.getProperty("server.mapping.share.password");
    }

    public static String getMainFolder() {
        return config.getProperty("server.mapping.main.folder");
    }

    public static void addHost(String id, String text, String port) {
        config.setProperty("host." + id, text + ":" + port);
    }

    public static Properties getConfig() {
        return config;
    }
}
